package com.woniuxy.store.app.apiImpl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.cloud.mall95.log.Log;
import com.woniuxy.store.app.util.StoreIdUtil;
import com.woniuxy.store.client.MemberLevelClient;
import com.woniuxy.store.client.param.AddMemberLevelParam;
import com.woniuxy.store.client.param.ModifyMemberLevelParam;
import com.woniuxy.store.client.result.MemberLevelDto;
import com.woniuxy.store.infra.model.MemberLevel;
import com.woniuxy.store.service.MemberLevelService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import result.Result;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/store/manage")
public class MemberLevelClientImpl implements MemberLevelClient {

    @Resource
    private MemberLevelService memberLevelService;

    @Log("添加商家会员规则")
    @Override
    public Result addMemberLevel(@Valid AddMemberLevelParam param) {
        MemberLevel level = BeanUtil.toBean(param, MemberLevel.class);
        System.out.println(StoreIdUtil.getFromToken()+"123");
        level.setStoreId(StoreIdUtil.getFromToken());
        memberLevelService.addMemberLevel(level);
        return Result.success();
    }

    @Override
    @Log("修改商家会员规则")
    public Result modifyMemberLevel(@Valid ModifyMemberLevelParam param) {
        MemberLevel level = BeanUtil.toBean(param, MemberLevel.class);
        level.setStoreId(StoreIdUtil.getFromToken());
        memberLevelService.modifyMemberLevel(level);
        return Result.success();
    }

    @Override
    @Log("删除会员规则")
    public Result delMemberLevel(Integer memberLevelId) {
        memberLevelService.delMemberLevel(StoreIdUtil.getFromToken(),memberLevelId);
        return Result.success();
    }

    @Override
    @Log("查询当前登录商家会员规则")
    public Result<List<MemberLevelDto>> getMemberLevel() {
        List<MemberLevel> list = memberLevelService.getMemberLevel(StoreIdUtil.getFromToken());
        List<MemberLevelDto> dtos = BeanUtil.copyToList(list, MemberLevelDto.class);
        return Result.success(dtos);
    }

    @Override
    @Log("根据商铺Id查询会员等级规则")
    public Result<List<MemberLevelDto>> getLevelById(Integer storeId) {
        List<MemberLevel> list = memberLevelService.list(new QueryWrapper<MemberLevel>().eq("store_id", storeId));
        List<MemberLevelDto> dtos = BeanUtil.copyToList(list, MemberLevelDto.class);
        return Result.success(dtos);
    }
}
